<template>
  <el-submenu
    v-if="menu.list && menu.list.length >= 1"
    :index="menu.menuId + ''"
    :popper-class="'site-sidebar--' + sidebarLayoutSkin + '-popper'"
    :class="menuActiveParentId == menu.menuId ? 'is-active' : ''"
  >
    <template slot="title">
      <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
      <!--<span>{{ menu.name }} {{menu.menuId}} {{menuActiveParentId}} </span>-->
      <span>{{ menu.name }}</span>
    </template>
    <sub-menu
      v-for="item in menu.list"
      :key="item.menuId"
      :menu="item"
      :dynamicMenuRoutes="dynamicMenuRoutes">
    </sub-menu>
  </el-submenu>
  <!--<el-menu-item v-else :index="menu.menuId + ''" @click="gotoRouteHandle(menu)">-->
  <!--<icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>-->
  <!--<span>{{ menu.name }}</span>-->
  <!--</el-menu-item>-->
  <!--20190119 根据当前标签，展开对应的菜单目录-->
  <el-menu-item v-else :index="menu.menuId + ''" @click="gotoRouteHandle(menu)"
                :class="menuActiveName == menu.menuId ? 'is-active' : ''">
    <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
    <!--<span>{{ menu.name }} {{menu.menuId}} {{menuActiveParentId}} </span>-->
    <span>{{ menu.name }}</span>
  </el-menu-item>
</template>

<script>
  import SubMenu from './main-sidebar-sub-menu'

  export default {
    name: 'sub-menu',
    props: {
      menu: {
        type: Object,
        required: true
      },
      dynamicMenuRoutes: {
        type: Array,
        required: true
      }
    },
    components: {
      SubMenu
    },
    computed: {
      // 侧栏颜色
      sidebarLayoutSkin: {
        get () {
          let skin = this.$store.state.theme.sidebarLayoutSkin.dark
          // console.log('skin', skin)
          if (skin) {
            return 'light'
          } else {
            return 'dark'
          }
        }
      },
      menuActiveParentId: {
        get () {
          return this.$store.state.common.menuActiveParentId
        },
        set (val) {
          this.$store.commit('common/updateMenuActiveParentId', val)
        }
      },
      // 20190119 根据当前标签，展开对应的菜单目录
      menuActiveName: {
        get () {
          return this.$store.state.common.menuActiveName
        },
        set (val) {
          this.$store.commit('common/updateMenuActiveName', val)
        }
      }
    },
    methods: {
      // 通过menuId与动态(菜单)路由进行匹配跳转至指定路由
      gotoRouteHandle (menu) {
        var route = this.dynamicMenuRoutes.filter(item => item.meta.menuId === menu.menuId)
        if (route.length >= 1) {
          this.$router.push({name: route[0].name})
        }
      }
    }
  }
</script>
